package com.mio.web.controller.user;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.durcframework.core.GridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.UserContext;
import org.durcframework.core.controller.CrudController;

import com.mio.web.common.MioConstants;
import com.mio.web.common.MioInfoTips;
import com.mio.web.common.MioUtil;
import com.mio.web.entity.user.User;
import com.mio.web.entity.user.UserSch;
import com.mio.web.service.user.UserService;
import com.mio.web.util.PasswordUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController extends CrudController<User, UserService> {

	// ������¼
	@RequestMapping("/addUser.do")
	public @ResponseBody MessageResult addUser(User entity,String year,String month,String day) {
		// pwd加密
		String newpwd = PasswordUtil.createHash(entity.getPassword());
		entity.setPassword(newpwd);
		entity.setCreatedTime(MioUtil.getYMDHMSDateTime(new Date()));
		entity.setBirthday(year+"."+month+"."+day);
		return this.save(entity);
	}

	// ɾ����¼
	@RequestMapping("/delUser.do")
	public @ResponseBody MessageResult delUser(User entity) {
		return this.delete(entity);
	}

	// �޸ļ�¼
	@RequestMapping("/updateUser.do")
	public @ResponseBody MessageResult updateUser(User entity,String year,String month,String day) {
		// 表示修改过密码
		if (entity.getPassword().length() <= 32) {
			// pwd加密
			String newpwd = PasswordUtil.createHash(entity.getPassword());
			entity.setPassword(newpwd);
		}
		if(year != null && month != null && day != null){
			entity.setBirthday(year+"."+month+"."+day);
		}
		
		return this.update(entity);
	}

	// ������ѯ��ҳ����
	@RequestMapping("/listUser.do")
	public @ResponseBody GridResult listUser(UserSch searchEntitySch) {
		return this.query(searchEntitySch);
	}

	// ������ѯ���������м�¼
	@RequestMapping("/listAllUser.do")
	public @ResponseBody Object listAllUser(UserSch searchEntitySch) {
		return this.queryAll(searchEntitySch);
	}

	// ��ȡ��ϸ��Ϣ
	@RequestMapping("/getUserById.do")
	public @ResponseBody User getUserById(User entity) {
		return this.getService().get(entity.getId());
	}

	@RequestMapping("/managerLogin.do")
	public @ResponseBody Map<String, Object> managerLogin(User user) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		User userEntity = this.getService().getUserByUserName(user.getUserName());
		if (userEntity == null) {
			resultMap.put("success", false);
			resultMap.put("info", MioInfoTips.USER_INFO_INCORRECT);
		} else {
			if (MioConstants.USER_TYPE_MANAGER == userEntity.getRoleProperty()) {
				boolean isPswdCorrect = PasswordUtil.validatePassword(user.getPassword(), userEntity.getPassword());
				if (isPswdCorrect) {
					UserContext.getInstance().setUser(userEntity);
					resultMap.put("success", true);
					resultMap.put("info", userEntity);
				} else {
					resultMap.put("success", false);
					resultMap.put("info", MioInfoTips.USER_INFO_INCORRECT);
				}
			} else {
				resultMap.put("success", false);
				resultMap.put("info", MioInfoTips.USER_INFO_INCORRECT);
			}

		}
		return resultMap;

	}

	/**
	 * 会员登录
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/login.do")
	public @ResponseBody Map<String, Object> login(User user) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//繁体字转换成简体
		String userName = null;
		String password = null;
		try {
			userName = new String(user.getUserName().getBytes("big5"),"utf-8");
			password = new String(user.getPassword().getBytes("big5"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User userEntity = this.getService().getUserByUserName(userName);
		if (userEntity == null) {
			resultMap.put("success", false);
			resultMap.put("info", MioInfoTips.USER_INFO_INCORRECT);
		} else {
			if (MioConstants.USER_TYPE_MEMBER == userEntity.getRoleProperty()) {
				boolean isPswdCorrect = PasswordUtil.validatePassword(password, userEntity.getPassword());
				if (isPswdCorrect) {
					UserContext.getInstance().setUser(userEntity);
					resultMap.put("success", true);
					resultMap.put("info", userEntity);
				} else {
					resultMap.put("success", false);
					resultMap.put("info", MioInfoTips.USER_INFO_INCORRECT);
				}
			} else {
				resultMap.put("success", false);
				resultMap.put("info", MioInfoTips.USER_INFO_INCORRECT);
			}

		}
		return resultMap;

	}

	@RequestMapping("/loginOut")
	public @ResponseBody void loginOut() {
		User user = UserContext.getInstance().getUser();
		if (user != null) {
			UserContext.getInstance().setUser(null);
		}
	}

	/**
	 * 忘记密码，通过邮件发送给用户新密码
	 * 
	 * @param user
	 * @param id
	 *            用户id主键
	 * @throws IOException
	 */
	@RequestMapping("/forgetPwd.do")
	public @ResponseBody Map<String, Object> forgetPwd(User user) throws IOException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean emailFlag = this.getService().sendEmail(user);
			if (emailFlag) {
				resultMap.put("success", true);
			} else {
				resultMap.put("success", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;

	}
}